package com.example.lagou_elasticsearch_homework.controller;

import com.example.lagou_elasticsearch_homework.es.model.PositionES;
import com.example.lagou_elasticsearch_homework.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

@Controller
public class PositionController {
    @Autowired
    private PositionService positionService;

    @GetMapping({"/","/index"})
    public   String  indexPage(){
        return  "index";
    }

    @RequestMapping("/importAll")
    @ResponseBody
    public String  importAll(){
        try {
            positionService.importAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  "success";
    }

    /**
     * 查询职位
     * @param positionName
     * @param pageNo
     * @param pageSize
     * @return
     * @throws IOException
     */
    @GetMapping("/search/{keyWord}/{pageNo}/{pageSize}")
    @ResponseBody
    public List<PositionES> searchPosition(@PathVariable("keyWord") String  positionName, @PathVariable("pageNo")int pageNo,
                                           @PathVariable("pageSize")int  pageSize)throws IOException {
        List<PositionES> list = positionService.findPositions(positionName, pageNo, pageSize);
        return  list;
    }
}
